/**
 * @Description
 * @Author LZL
 * @Date 2025/2/25
 */


package com.kcb.coursemanager;

import com.kcb.connectDB.connectDB;
import com.kcb.choose.choose2;

import javax.swing.*;
import java.awt.*;

public class addcourse extends JFrame {
    private JTextField courseNameField, teacherNameField, timeField, locationField;
    private JButton addButton, backButton;
    private String username;

    public addcourse(String username) {
        this.username = username;
        setTitle("城科课程表管理系统:课程表添加功能");
        setSize(450, 350);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        ImageIcon icon = new ImageIcon("C:\\Users\\TBS\\IdeaProjects\\KCB\\image\\ccst.png");
        if (icon != null) {
            setIconImage(icon.getImage());
        } else {
            System.out.println("Failed to load icon image.");
        }


        // 主面板
        JPanel panel = new JPanel();
        panel.setBackground(new Color(240, 245, 255));
        panel.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10);
        gbc.fill = GridBagConstraints.HORIZONTAL;

        JLabel titleLabel = new JLabel("添加课程");
        titleLabel.setFont(new Font("宋体", Font.BOLD, 22));
        titleLabel.setForeground(new Color(50, 50, 150));

        // 课程信息输入框
        courseNameField = createStyledTextField();
        teacherNameField = createStyledTextField();
        timeField = createStyledTextField();
        locationField = createStyledTextField();

        // 按钮
        addButton = createStyledButton("添加课程", new Color(60, 160, 250));
        backButton = createStyledButton("返回", new Color(120, 120, 120));

        // 布局
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 2;
        gbc.anchor = GridBagConstraints.CENTER;
        panel.add(titleLabel, gbc);

        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.WEST;

        addLabel(panel, gbc, "课程名称:", 1);
        addTextField(panel, gbc, courseNameField, 1);

        addLabel(panel, gbc, "教师名称:", 2);
        addTextField(panel, gbc, teacherNameField, 2);

        addLabel(panel, gbc, "时间:", 3);
        addTextField(panel, gbc, timeField, 3);

        addLabel(panel, gbc, "地点:", 4);
        addTextField(panel, gbc, locationField, 4);

        gbc.gridy = 5;
        gbc.gridx = 0;
        panel.add(backButton, gbc);

        gbc.gridx = 1;
        panel.add(addButton, gbc);

        getContentPane().add(panel);

        // 按钮监听
        addButton.addActionListener(e -> addCourse());
        backButton.addActionListener(e -> {
            new choose2(username).setVisible(true);
            dispose();
        });
    }

    private void addLabel(JPanel panel, GridBagConstraints gbc, String text, int y) {
        gbc.gridx = 0;
        gbc.gridy = y;
        panel.add(new JLabel(text), gbc);
    }

    private void addTextField(JPanel panel, GridBagConstraints gbc, JTextField textField, int y) {
        gbc.gridx = 1;
        gbc.gridy = y;
        panel.add(textField, gbc);
    }

    private JTextField createStyledTextField() {
        JTextField textField = new JTextField(15);
        textField.setFont(new Font("宋体", Font.PLAIN, 16));
        textField.setPreferredSize(new Dimension(180, 30));
        return textField;
    }

    private JButton createStyledButton(String text, Color bgColor) {
        JButton button = new JButton(text);
        button.setFont(new Font("宋体", Font.BOLD, 16));
        button.setBackground(bgColor);
        button.setForeground(Color.WHITE);
        button.setFocusPainted(false);
        button.setBorderPainted(false);
        button.setPreferredSize(new Dimension(180, 45));
        return button;
    }

    private void addCourse() {
        String courseName = courseNameField.getText().trim();
        String teacherName = teacherNameField.getText().trim();
        String time = timeField.getText().trim();
        String location = locationField.getText().trim();

        if (courseName.isEmpty() || teacherName.isEmpty() || time.isEmpty() || location.isEmpty()) {
            JOptionPane.showMessageDialog(null, "请填写所有字段！", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        }

        connectDB dbConnector = new connectDB();
        if (dbConnector.addCourse(username, courseName, teacherName, time, location)) {
            JOptionPane.showMessageDialog(null, "课程添加成功！");
            new choose2(username).setVisible(true);
            dispose();
        } else {
            JOptionPane.showMessageDialog(null, "添加课程失败。");
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new addcourse("exampleUser").setVisible(true));
    }
}
